package GUI;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import logic.Member;
import logic.Message;

import java.awt.Color;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Calendar;
import javax.swing.UIManager;


public class ViewMember extends JPanel {

		private Client_GUI parent;
		private JLabel memberName;
		private JTextField idLBL;
		private JTextField dobLBL;
		private JTextField phoneLBL;
		private JTextField mailLBL;
		private JTextField addressLBL;
		private Member member;
		private JLabel L1;
		private JLabel L2;
		private JLabel L3;
		private JLabel L4;
		private JButton btnSearch;
		private JButton btnFreze;
		
		private boolean frozen = false;
		
		private JLabel id;

		private boolean editing = false;
		
		private ArrayList<String> index ;
		private ArrayList<Object> data ;
		private ResultDisplay results;
		
		public ViewMember(final Client_GUI parent,final Member member,final ResultDisplay results) {
			this.parent = parent;
			this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
			setLayout(null);
			this.setBackground(UIManager.getColor("inactiveCaption"));

			this.member = member;
			this.results = results;
			
			
			
			
			memberName = new JLabel("memberName");
			memberName.setForeground(Color.RED);
			memberName.setFont(new Font("Tahoma", Font.BOLD, 18));
			memberName.setBounds(320, 11, 168, 47);
			add(memberName);
			
			id = new JLabel("Student ID:");
			id.setFont(new Font("Dialog", Font.BOLD, 13));
			id.setBounds(229, 173, 144, 14);
			add(id);
			
			JLabel phone = new JLabel("Phone:");
			phone.setFont(new Font("Dialog", Font.BOLD, 13));
			phone.setBounds(229, 224, 144, 14);
			add(phone);
			
			JLabel dob = new JLabel("Date Of Birth:");
			dob.setFont(new Font("Dialog", Font.BOLD, 13));
			dob.setBounds(229, 199, 144, 14);
			add(dob);
			
			JLabel mail = new JLabel("Mail:");
			mail.setFont(new Font("Dialog", Font.BOLD, 13));
			mail.setBounds(229, 249, 144, 14);
			add(mail);
			
			JLabel address = new JLabel("Address:");
			address.setFont(new Font("Dialog", Font.BOLD, 13));
			address.setBounds(229, 274, 144, 14);
			add(address);
			
		    btnSearch = new JButton("Edit");
			btnSearch.setFont(new Font("Tahoma", Font.BOLD, 13));
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {	
					
					phoneLBL.setEditable(true);
					mailLBL.setEditable(true);
					addressLBL.setEditable(true);
					dobLBL.setEnabled(true);
					btnSearch.setText("Update");
					
					ValidateData(editing);
					editing = true;
					
				}
			});
			btnSearch.setBounds(194, 436, 131, 34);
			add(btnSearch);
			
			
			JButton btnCancel = new JButton("Back");
			btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
			btnCancel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					parent.RepaintMain(results);
					
				}
			});
			btnCancel.setBounds(457, 436, 131, 34);
			add(btnCancel);
			
			idLBL = new JTextField("");
			idLBL.setFont(new Font("Dialog", Font.BOLD, 14));
			idLBL.setBounds(365, 173, 186, 24);
			add(idLBL);
			idLBL.setEnabled(false);
			
			dobLBL = new JTextField("");
			dobLBL.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent arg0) {
					ValidateData(false);
				}
			});
			dobLBL.setFont(new Font("Dialog", Font.BOLD, 14));
			dobLBL.setBounds(365, 198, 186, 24);
			add(dobLBL);
			dobLBL.setEnabled(false);
			
			phoneLBL = new JTextField("");
			phoneLBL.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent arg0) {
					ValidateData(false);
				}
			});
			
			phoneLBL.setFont(new Font("Dialog", Font.BOLD, 14));
			phoneLBL.setBounds(365, 224, 186, 24);
			add(phoneLBL);
			phoneLBL.setEnabled(false);
			
			mailLBL = new JTextField("");
			mailLBL.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent arg0) {
					ValidateData(false);
				}
			});
			
			mailLBL.setFont(new Font("Dialog", Font.BOLD, 14));
			mailLBL.setBounds(365, 249, 186, 24);
			add(mailLBL);
			mailLBL.setEnabled(false);
			
			addressLBL = new JTextField("");
			addressLBL.addFocusListener(new FocusAdapter() {
				@Override
				public void focusLost(FocusEvent arg0) {
					ValidateData(false);
				}
			});
			
			addressLBL.setFont(new Font("Dialog", Font.BOLD, 14));
			addressLBL.setBounds(365, 274, 186, 24);
			add(addressLBL);
			addressLBL.setEnabled(false);
			
			L1 = new JLabel("");
			L1.setBounds(554, 202, 20, 20);
			add(L1);
			
			L2 = new JLabel("");
			L2.setBounds(554, 225, 20, 20);
			add(L2);
			
			L3 = new JLabel("");
			L3.setBounds(554, 250, 20, 20);
			add(L3);
			
			L4 = new JLabel("");
			L4.setBounds(554, 275, 20, 20);
			add(L4);
			
			btnFreze = new JButton("Freeze Member Cards");
			btnFreze.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0){
					
					index = new ArrayList<String>();
					data = new ArrayList<Object>();
					int action = Message.FREEZE_MEMBER_REQUEST;

					index.add("UID");
					data.add(member.getUID());
					index.add("STATUS");
					
					if (frozen){
						data.add(Member.VALID_MEMBERCARD);
					}else{	
						data.add(Member.FROZEN_MEMBERCARD);
					}
					
				
				
					Message toSend = new Message(index, data, action);
					
					if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
						parent.safeToClear = true;
						parent.RepaintMenu(parent.client.Privelege);
					
					}else{//message send failed
						//TODO verify extream situation
					}
				
				}
			});
			btnFreze.setForeground(Color.RED);
			btnFreze.setFont(new Font("Tahoma", Font.BOLD, 15));
			btnFreze.setBackground(Color.BLUE);
			btnFreze.setBounds(291, 346, 209, 34);
			add(btnFreze);
			if (parent.client.Privelege!=Member.MANAGER_PRIVILIGE){
				btnFreze.setVisible(false);
			}
			InitMemberData();
			
		}
		
		private void InitMemberData(){
			memberName.setText(member.getName());
			idLBL.setText(String.valueOf(member.getUID()));
			dobLBL.setText(member.getDOB());
			phoneLBL.setText(member.getPhone());
			mailLBL.setText(member.getMail());
			addressLBL.setText(member.getAddress());
			
			if ((member.getStatus() == 0) && (parent.client.Privelege == Member.MANAGER_PRIVILIGE)){
				btnFreze.setVisible(false);
				btnSearch.setVisible(false);
				id.setText("Worker ID:");
			}
			
			if (member.getStatus() == Member.VALID_MEMBERCARD){
				frozen = false;
			}else if (member.getStatus() == Member.FROZEN_MEMBERCARD){
				frozen = true;
				btnFreze.setText("Unfreeze member card");
			}
		}

		
		
		
		private void ValidateData(boolean fromOK)
		{
			
			boolean valid = true;
			
			String temp; // the string to be tested on each test
		
			
			ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
			ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
			
			ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
			ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
			
			
			//validate DOB
			temp = dobLBL.getText();
			if(!temp.equals(""))
			try
			{
				if ((temp.charAt(4) == '-' && temp.charAt(7) == '-') || (temp.charAt(4) == '/' && temp.charAt(7) == '/') || (temp.charAt(4) == '.' && temp.charAt(7) == '.')) 
					L1.setIcon(OK);
				else {
					L1.setIcon(BAD);
					valid = false;
				}
				
				int year = Integer.parseInt(temp.substring(0, 4));
				int month = Integer.parseInt(temp.substring(5, 7));
				int day = Integer.parseInt(temp.substring(8, 10));

				Calendar validator = Calendar.getInstance();
				validator.set(year, month-1, day);
				validator.setLenient(false);
				validator.getTime();
				
			}catch(NumberFormatException e){
				L1.setIcon(BAD);
				valid = false;
			}catch(Exception e){
				L1.setIcon(BAD);
				valid = false;
			}
			else L1.setIcon(null);
			
			
			
			//validate phone
			if (!phoneLBL.getText().equals("")){
				String phone = phoneLBL.getText();
				phone = phone.replace("-", "");
				phone = phone.replace(" ", "");
				
				try{
					Integer.parseInt(phone);
					L2.setIcon(OK);
				}catch(NumberFormatException e){
					L2.setIcon(BAD);
					valid = false;
				}
				if(phoneLBL.getText().length()!=9 && phoneLBL.getText().length()!=10
						&& phoneLBL.getText().length()!=11)
		{
				L2.setIcon(BAD);
			valid = false;
		}
			}
			else L2.setIcon(null);
			
			//validate Email
			if (!mailLBL.getText().equals("")){
				int schtroodelLocation = mailLBL.getText().indexOf("@");
				
				if (mailLBL.getText().indexOf("@", schtroodelLocation) == -1){//another @ exists
					L3.setIcon(BAD);
					valid = false;
				}else if (mailLBL.getText().indexOf(".", schtroodelLocation) == -1){
					L3.setIcon(BAD);
					valid = false;
				}else if (schtroodelLocation == 0){
					L3.setIcon(BAD);
					valid = false;
				}else L3.setIcon(OK);
			}
			else L3.setIcon(null);
			
		
			
			//validate address
			if (!addressLBL.getText().equals("")) L4.setIcon(OK);
			else L4.setIcon(null);
			
			
			
			
			
			if (fromOK)
			{
				if (mailLBL.getText().equals("")){
					L3.setIcon(BAD);
					valid = false;
				}
				if (phoneLBL.getText().equals("")){
					L2.setIcon(BAD);
					valid = false;
				}
				if (addressLBL.getText().equals("")){
					L4.setIcon(BAD);
					valid = false;
				}
				if (dobLBL.getText().equals("")){
					L1.setIcon(BAD);
					valid = false;
				}
				
			}
			
			if (valid && fromOK){
				int action = Message.UPDATE_MEMBER_REQUEST;
				index = new ArrayList<String>();
				data = new ArrayList<Object>();
				
		
				index.add("UID");
				data.add(member.getUID());
		
				index.add("DOB");
				data.add(dobLBL.getText());
				
				index.add("PHONE");
				data.add(phoneLBL.getText());
				
				index.add("MAIL");
				data.add(mailLBL.getText());
				
				index.add("ADDRESS");
				data.add(addressLBL.getText());
				
				Message toSend = new Message(index, data, action);
				
				if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
					parent.RepaintMenu(parent.client.Privelege);
					
				}else{//message send failed
					//TODO verify extream situation
				}
				
			}
		}
}
